package de.tabuma.adminarea;

import de.tabuma.lib.*;
import de.tabuma.lib.contentmanagement.entity.*;

import java.io.*;
import java.util.*;
import java.util.logging.*;
import java.lang.reflect.*;
import javax.servlet.*;
import javax.servlet.http.*;

import com.google.appengine.api.users.*;

/**
 * @author $Author$
 * @version $Revision$
 * @since $Date$
 */
public class controller extends tabumacontroller {

    private static final Logger log = Logger.getLogger(controller.class.getName());

    public controller(String servlet, Hashtable hashtable) {
        super(servlet, hashtable);
    }

    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        try {
            UserService userService = UserServiceFactory.getUserService();
            com.google.appengine.api.users.User user = userService.getCurrentUser();
                
            String header = "admin settings";
            out.println("\t\t\t<div class=\"contentHeader\">"+header+"</div>");
            out.println("\t<div class=\"contentContent\">");

            if(userService.isUserAdmin()) {


                out.println("<p class=\"contentContentHeader\">"+header+"</p>");

                String key = "key";
                String value = "value";

                String entityName = "admin settings";

                Content content = new Content();

                Vector<Field> editableFields = new Vector<Field>();
                editableFields.add(de.tabuma.adminarea.AdminSetting.class.getDeclaredField("key"));
                editableFields.add(de.tabuma.adminarea.AdminSetting.class.getDeclaredField("value"));
                content.FormContent.setEditableFields(editableFields);

                Vector<Integer> editableTypes = new Vector<Integer>();
                editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_STRING));
                editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_STRING));
                content.FormContent.setEditableFieldTypes(editableTypes);
                content.FormContent.setEntityClass(de.tabuma.adminarea.AdminSetting.class);
                content.FormContent.setEntityName(entityName);
                content.FormContent.setFacadeObject(new AdminSettingFacade());

                content.ListContent.setListParameterValue("");

                Vector<Field> staticFields = new Vector<Field>();
                content.FormContent.StaticContent.setStaticFields(staticFields);
                Vector<Integer> staticFieldsType = new Vector<Integer>();
                content.FormContent.StaticContent.setStaticFieldsTyp(staticFieldsType);
                Vector<String> staticFieldsValue = new Vector<String>();
                content.FormContent.StaticContent.setStaticFieldsValue(staticFieldsValue);

                content.GeneralContent.setParamName(constants.ADMIN_AREA_PARAM);

                Vector<Field> listableFields = new Vector<Field>();
                listableFields.add(de.tabuma.adminarea.AdminSetting.class.getDeclaredField("key"));
                listableFields.add(de.tabuma.adminarea.AdminSetting.class.getDeclaredField("value"));
                content.ListContent.setListableFields(listableFields);

                Hashtable<String,Hashtable<String,String>> additionalListFields = new Hashtable<String,Hashtable<String,String>>();
                content.ListContent.setAdditionalListFields(additionalListFields);

                content.GeneralContent.setServlet(this.getServlet());
                content.GeneralContent.setParameter(this.getParameter());

                Hashtable<Field, String> displayFields = new Hashtable<Field, String>();
                displayFields.put(de.tabuma.adminarea.AdminSetting.class.getDeclaredField("key"), key);
                displayFields.put(de.tabuma.adminarea.AdminSetting.class.getDeclaredField("value"), value);
                content.FormContent.setDisplayedName(displayFields);

                Hashtable<Field, List> listParameterContent = new Hashtable<Field, List>();
                content.ListContent.setListParameterContent(listParameterContent);

                Hashtable<Field,List<Field>> listParameterContentDisplayedFields = new Hashtable<Field, List<Field>>();
                content.ListContent.setListParameterDisplayedField(listParameterContentDisplayedFields);

                content.GeneralContent.setUser(user.getEmail());

                out.println(content.navigate(request));
            }
            else {
                out.println("You are not allowed to see this page.");
            }
            out.println("</div>");
        }
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
    }

}
